package helper

import (
	"crypto/md5"
	"encoding/base64"
	"fmt"
	"github.com/google/uuid"
	"net/url"
	"strings"
	"time"
	"unicode/utf8"
)

const (
	TIME_ZONE    = "GMT"
	FORMAT_ISO_8601 = "2006-01-02T15:04:05Z"
	FORMAT_RFC_2616 = "Mon, 02 Jan 2006 15:04:05 GMT"
)

func GetUUID() string {
	return uuid.New().String()
}

func GetISO8601Date() string {
	return time.Now().UTC().Format(FORMAT_ISO_8601)
}

func GetRFC2616Date() string {
	return time.Now().UTC().Format(FORMAT_RFC_2616)
}

func MD5Sum(content []byte) string {
	h := md5.New()
	h.Write(content)
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

func PercentEncode(encodeStr string) string {
	encodeStr = fmt.Sprint(encodeStr)
	if utf8.RuneCountInString(encodeStr) == len(encodeStr) {
		return url.QueryEscape(encodeStr)
	}
	if utf8.RuneCountInString(encodeStr) > len(encodeStr) {
		res := url.QueryEscape(encodeStr)
		res = strings.ReplaceAll(res, "+", "%20")
		res = strings.ReplaceAll(res, "*", "%2A")
		res = strings.ReplaceAll(res, "%7E", "~")
		return res
	}
	return ""
}

